package com.petecapra.cashflow.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;

import com.petecapra.cashflow.model.dao.AccountTransactionManager;

@Entity
@Table(name="tblAccountTransactions")
public class AccountTransaction extends BaseDomainObject<AccountTransactionManager> implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	@NotNull
    @ManyToOne(targetEntity = Schedule.class)
    @JoinColumn
    private Schedule schedule;
    
    @NotNull
    @DateTimeFormat(style="S-")
    private Date due;

    private boolean paid;
    
    @Transient
	@Autowired
	private AccountTransactionManager accountTransactionManager;
    
    public AccountTransaction() {}
    
    public AccountTransaction(Schedule schedule, Date due) {
    	this.schedule = schedule;
    	this.due = due;
    	this.paid = false;
    }

    public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public Date getDue() {
		return due;
	}

	public void setDue(Date due) {
		this.due = due;
	}

	public boolean isPaid() {
		return paid;
	}

	public void setPaid(boolean paid) {
		this.paid = paid;
	}

	public Schedule getSchedule() {
		return schedule;
	}

	public void setSchedule(Schedule schedule) {
		this.schedule = schedule;
	}

	public void markPaid() {
		setPaid(true);
		schedule.payTransaction();
		schedule.getAccount().save();
	}

	@Override
	public AccountTransactionManager getManager() {
		return accountTransactionManager;
	}

}